package com.cskaoyan.controller;

import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.PO.MarketUser;
import com.cskaoyan.bean.VO.UserInfoBean;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.service.WxAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName WxAuthController
 * @Description
 * @Author Lxz
 * @Date 2022-8-15 20:27
 */

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {
    @Autowired
    WxAuthService wxAuthService;


    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map, HttpServletRequest request) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        Subject subject = SecurityUtils.getSubject();
        MarketToken token = new MarketToken(username, password, "wx");
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        if (!subject.isAuthenticated()) {
            return BaseRespVo.invalidParameter("登录失败");
        }
        Session session = subject.getSession();
        session.setAttribute("username",username);
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        MarketUser userInfo = wxAuthService.getUserInfo(primaryPrincipal);
        session.setAttribute("userId", userInfo.getId());

        // 记录登录时间和ip
        String remoteAddr = request.getRemoteAddr();
        primaryPrincipal.setLastLoginIp(remoteAddr);
        wxAuthService.updateLog(primaryPrincipal);

        LoginUserData userData = new LoginUserData();
        userData.setToken((String) session.getId());
        UserInfoBean userInfoBean = new UserInfoBean();
        userInfoBean.setNickName(userInfo.getNickname());
        userInfoBean.setAvatarUrl(userInfo.getAvatar());
        userData.setUserInfo(userInfoBean);
//        LoginUserData userData = new LoginUserData();
//        userData.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIG1hcmtldCB0b2tlbiIsImF1ZCI6Ik1JTklBUFAiLCJpc3MiOiJNQVJLRVQiLCJleHAiOjE2NjA1NzMyODAsInVzZXJJZCI6MSwiaWF0IjoxNjYwNTY2MDgwfQ.HhMXMLeL4Belo_mu4Ay4BTyaVGWQh0BYl9VKiLGxz-o");
//        UserInfoBean userInfoBean = new UserInfoBean();
//        userInfoBean.setAvatarUrl("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        userInfoBean.setNickName("user123");
//        userData.setUserInfo(userInfoBean);

        return BaseRespVo.ok(0, userData, "成功");
    }


    @RequestMapping("logout")
    public BaseRespVo logout(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok(0, null, "成功");
    }

}
